﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Runtime.InteropServices;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using SetSailCommon;
using SetSailControl;
using SetSailControl.Controls;
using SetSailModel;
using SetSailDal;

namespace SetSailView
{
    public partial class Frm_Main : Form
    {
        public Frm_Main()
        {
            InitializeComponent();
        }

        /// <summary>
        /// 圆角
        /// </summary>
        /// <param name="e"></param>
        protected override void OnSizeChanged(EventArgs e)
        {
            base.OnSizeChanged(e);
            RenderHelper.SetFormRoundRectRgn(this, 30);
        }

        private string CurrMenu;//当前菜单
        private void Frm_Main_Load(object sender, EventArgs e)
        {
            //开窗起始位置
            this.StartPosition = FormStartPosition.Manual;
            this.Location = new Point(SystemInformation.PrimaryMonitorSize.Width / 2 - 491, SystemInformation.PrimaryMonitorSize.Height / 2 - 309);

            ucNavigationMenu1.ForeColor = Color.White;
            tableLayoutPanel1.BackColor = Color.FromArgb(255, 224, 192);
            LbShowInfo.ForeColor = Color.FromArgb(192, 0, 0);

            //ucNavigationMenu1.Controls[8].Visible = false;
            //默认菜单
            AddControl(new Frm_LYDD(), "联运读档");

            //公告
            var result = MainDal.Query_LYGG("提醒");
            if (result.Code == ResultCode.Success && result.Data != null)
            {
                LbShowInfo.Text = result.Data.GGNR;
            }
            else
            {
                LbShowInfo.Text = "暂无...";
            }

            label2.Text = "当前版本号：" + Application.ProductVersion.ToString();


            //根据用户类型判断加载菜单
            //    foreach (NavigationMenuItem item in ucNavigationMenu1.Items)
            //    {
            //        switch (CookieData.getInstance().YHXX.ZHLX)
            //        {
            //            case "user"://用户
            //                switch (item.Text.Trim())
            //                {
            //                    case ""://logo和图标的Text为空
            //                    case "联运读档":
            //                    case "信息变更":
            //                        break;
            //                    default:
            //                        break;
            //                }
            //                break;
            //            case "admin"://管理

            //                break;
            //            case "sys"://后台管理

            //                break;
            //        }
            //    }
        }


        //window标准拖拽事件
        [DllImport("user32.dll", EntryPoint = "ReleaseCapture")]
        public static extern void ReleaseCapture();
        [DllImport("user32.dll", EntryPoint = "SendMessage")]
        public static extern void SendMessage(int hwnd, int wMsg, int wParam, int lParam);


        //绘制单元格边框
        private void tableLayoutPanel1_CellPaint(object sender, TableLayoutCellPaintEventArgs e)
        {
            Pen cpen = new Pen(Color.Black);
            cpen.Width = 1F;
            Rectangle rectangle = new Rectangle(e.CellBounds.X, e.CellBounds.Y, e.CellBounds.X + this.Width, e.CellBounds.Y + this.Height);
            e.Graphics.DrawRectangle(cpen, rectangle);
        }

        private void ucNavigationMenu1_ClickItemed(object sender, EventArgs e)
        {
            if (ucNavigationMenu1.SelectItem != null)
            {
                //MessageBox.Show("点击了:" + ucNavigationMenu1.SelectItem.Text);
                if (ucNavigationMenu1.SelectItem.Text == null) return;
                string MenuName = ucNavigationMenu1.SelectItem.Text.Trim();
                if (MenuName == CurrMenu) return;
                if (!string.IsNullOrWhiteSpace(MenuName)) panControl.Controls.Clear();
                switch (MenuName)
                {
                    //case "首页":
                    //    AddControl(new Frm_HOME(), MenuName);
                    //    break;
                    case "联运读档":
                        AddControl(new Frm_LYDD(), MenuName);
                        break;
                    case "信息变更":
                        AddControl(new Frm_XXBG(), MenuName);
                        break;
                    case "联运存档":
                        if (CookieData.getInstance().YHXX.ZHLX == "user")
                            AddControl(new Frm_Default(), MenuName);
                        else
                            AddControl(new Frm_LYCD(), MenuName);
                        break;
                    case "存档记录":
                        if (CookieData.getInstance().YHXX.ZHLX == "user")
                            AddControl(new Frm_Default(), MenuName);
                        else
                            AddControl(new Frm_CDJL(), MenuName);
                        break;
                    case "读档记录":
                        if (CookieData.getInstance().YHXX.ZHLX == "user")
                            AddControl(new Frm_Default(), MenuName);
                        else
                            AddControl(new Frm_DDJL(), MenuName);
                        break;
                    case "用户管理":
                        if (CookieData.getInstance().YHXX.ZHLX == "user")
                            AddControl(new Frm_Default(), MenuName);
                        else
                            AddControl(new Frm_YHGL(), MenuName);
                        break;
                    case "后台审核":
                        if (CookieData.getInstance().YHXX.ZHLX == "user")
                            AddControl(new Frm_Default(), MenuName);
                        else
                            AddControl(new Frm_HTSH(), MenuName);
                        break;
                    case "审核记录":
                        if (CookieData.getInstance().YHXX.ZHLX == "user")
                            AddControl(new Frm_Default(), MenuName);
                        else
                            AddControl(new Frm_SHJL(), MenuName);
                        break;
                    case "加速器记录":
                        if (CookieData.getInstance().YHXX.ZHLX == "user")
                            AddControl(new Frm_Default(), MenuName);
                        else
                            AddControl(new Frm_JSQJL(), MenuName);
                        break;
                    case "软件升级":
                        if (CookieData.getInstance().YHXX.ZHLX != "sys")
                            AddControl(new Frm_Default(), MenuName);
                        else
                            AddControl(new Frm_RJSJ(), MenuName);
                        break;
                    case "字典管理":
                        if (CookieData.getInstance().YHXX.ZHLX != "sys")
                            AddControl(new Frm_Default(), MenuName);
                        else
                            AddControl(new Frm_ZDGL(), MenuName);
                        break;
                    case "":
                        switch (ucNavigationMenu1.SelectItem.TipText.Trim())
                        {
                            case "最小化":
                                this.WindowState = FormWindowState.Minimized;
                                break;
                            case "关闭":
                                Application.ExitThread();
                                break;
                        }
                        break;
                }
            }
        }

        private void AddControl(Control c, string MenuName)
        {
            this.panControl.Controls.Add(c);

            CurrMenu = MenuName;
        }

        private void ucNavigationMenu1_MouseDown(object sender, MouseEventArgs e)
        {
            if (e.Button == MouseButtons.Left)
            {
                ReleaseCapture();
                SendMessage((int)this.Handle, 0xA1, 2, 0);
                //Console.WriteLine(ActionServo1.Location.X+"--"+ ActionServo1.Location.Y);
            }
        }

        /// <summary>
        /// QQ超链接
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void linkLabel1_LinkClicked(object sender, LinkLabelLinkClickedEventArgs e)
        {
            System.Diagnostics.Process.Start("http://wpa.qq.com/msgrd?v=3&uin=1446961432&site=qq&menu=yes");
        }
        bool flag_timer = false;
        /// <summary>
        /// 公告
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void timer1_Tick(object sender, EventArgs e)
        {
            if (flag_timer)
            {
                LbShowInfo.ForeColor = Color.FromArgb(192, 0, 0);
                flag_timer = false;
            }
            else
            {
                LbShowInfo.ForeColor = Color.Blue;
                flag_timer = true;
            }
        }

        private void Frm_Main_Paint(object sender, PaintEventArgs e)
        {
            e.Graphics.SmoothingMode = System.Drawing.Drawing2D.SmoothingMode.AntiAlias;
            e.Graphics.InterpolationMode = System.Drawing.Drawing2D.InterpolationMode.HighQualityBicubic;
            e.Graphics.CompositingQuality = System.Drawing.Drawing2D.CompositingQuality.HighQuality;
        }
    }
}
